package com.jy.modules.cims.component.image.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.SpringContextUtils;
import com.jy.modules.cims.component.common.ImgurlUtils;
import com.jy.modules.cims.component.image.TransferImagesDto;
import com.jy.modules.cims.component.image.TransferImagesQueue;
import com.jy.modules.cims.component.tbj.TBJBusinessClient;
import com.jy.modules.cims.data.dao.AttachDao;
import com.jy.modules.cims.data.dto.inter.tbj.TBJMessage;
import com.jy.modules.cims.data.dto.inter.tbj.jj.AttachContent;
import com.jy.modules.cims.data.dto.inter.tbj.jj.AttachFile;
import com.jy.modules.cims.data.dto.inter.tbj.jj.AttachLoan;

/**
 * @description 发送附件服务
 * @author zhangmi
 * @date 2017年2月8日
 */
@Component
public class SendAttachService extends Observable {
	private static Logger logger = LoggerFactory.getLogger(SendAttachService.class);
	
	@Autowired
	private SpringContextUtils springContextUtils;
	
    @Autowired
    private AttachDao attachDao;
    
    @Autowired
    @Qualifier("com.jy.modules.cims.component.tbj.TBJBusinessClient")
    private TBJBusinessClient tbj;
    
    @Autowired
    @Qualifier("com.jy.modules.cims.component.common.ImgurlUtils")
    private ImgurlUtils imgurlUtils;
	/**
	 * 铜板街发送影像附件列表
	 * @author zhangmi
	 * @date  2017年2月8日
	 * @param transferImagesDto
	 */
	public void doSomething(TransferImagesDto transferImagesDto) {
		String contractNo = transferImagesDto.getContractNo();
		try{
			logger.debug("发送铜板街附件列表信息》》开始:"+contractNo);
			List<AttachContent> list = attachDao.findAttachContentContractNo(contractNo);
			if(CollectionsUtils.isEmpty(list)){
				logger.info("发送铜板街附件列表信息》查询数据为空！");
				return;
			}
			 for (AttachContent attachContent : list) {
		        List<AttachFile> files = attachDao.findAttachFile(attachContent);
		        for(AttachFile a :files){
		            //计算文件名字
		            a.setFileCategory(imgurlUtils.getAttachType(a.getFileName()));
		            a.setFileType(1);//图片类型
		        }
		        //
		        AttachLoan attachLoan = new AttachLoan();
		        attachLoan.setLoanNumber(attachContent.getStreamId());
		        attachLoan.setFile(files);
		        List<AttachLoan> loan = new ArrayList<AttachLoan>();
		        loan.add(attachLoan);
		        attachContent.setLoan(loan);
		        // 推送铜板街
		        TBJMessage tBJMessage = tbj.addAttach(attachContent);
		        if (tBJMessage.getResult().equals("1")) {// 上传成功更新本地状态
		        	attachDao.updateStatus(attachLoan);
		            logger.debug("发送铜板街附件列表信息成功"+attachLoan.getLoanNumber());
					TransferImagesQueue.borrowerInfoQueue.offer(transferImagesDto);
		        }else{
		            logger.debug("发送铜板街附件列表信息失败"+attachLoan.getLoanNumber());
		        }
	           
		    }
		} catch (Exception e) {
			logger.error("发送铜板街附件列表异常："+contractNo+":"+e.getMessage());
		} 
	}
}
